// pages/welfare-list/welfare-list.js
import {Welfare} from '../../../models/welfare'
import {Location} from '../../../util/location'
import {check} from "../../../util/identityCheck";
import distribution from '../../../util/distribution'

const location = new Location()
let redPack = new Welfare()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        redPacketInfo: [],
        page: 0,
        size: 10,
        showLoading: true,
        address: '全国',
        showSkeleton: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        distribution(options)
        if(!check())return
        this.getAddress()
    },

    getAddress() {
        location.reverseGeocoder().then(res => {
            console.log(res)
            if(res.city){
                this.setData({
                    address: res.city.substring(0, res.city.length - 1)
                })
            }
            wx.setStorageSync('address', res)
            this._getRedPacket()
        })
    },

    _getRedPacket(page, type) {
        let params = {
            userId: wx.getStorageSync('userInfo').user_id || 0,
            page: this.data.page,
            size: this.data.size,
            address: this.data.address
        }
        redPack.getRedPacket(params, res => {
            console.log(res);
            /*
            * 获取红包信息
            * */
            this.data.redPacketInfo.push(...res)
            this.setData({
                redPacketInfo: this.data.redPacketInfo
            })
            // 返回的总数据为30并且返回10个数据
            if (this.data.redPacketInfo.length % 10 === 0 && res.length !== 0) {
                this.setData({
                    showLoading: true
                })
            } else {
                this.setData({
                    showLoading: false
                })
            }
            this.setData({
                showSkeleton: false
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    toDetail(event) {
        console.log(event)
        console.log(event.currentTarget.dataset.packinfoitem.id)
        let redPackId = event.currentTarget.dataset.packinfoitem.id
        wx.navigateTo({
            url: `/pages/welfare-detail/welfare-detail?id=${redPackId}`
        })
    },

    toRelease() {
        wx.navigateTo({
            url: '/pages/welfare-release/welfare-release'
        })
    },
    toRedPack() {
        wx.navigateTo({
            url: '/pages/my-balance-detail/my-balance-detail'
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
        console.log('上拉刷新')
        this.setData({
            page: 0,
            redPacketInfo: []
        })
        this._getRedPacket('', 'downRefresh')
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        console.log(this.data.redPacketInfo.length);
        if (this.data.redPacketInfo.length % 10 === 0) {
            console.log('上拉触底');
            this.setData({
                page: this.data.page + 1
            })
            this._getRedPacket()
        } else {
            console.log('上拉触底2');
        }

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let timestamp = Date.parse(new Date())/1000
        return {
            path: `/pages/welfare-list/welfare-list?otherId=${wx.getStorageSync('userInfo').user_id}&time=${timestamp}&otherCode=${wx.getStorageSync('shareCode')}`
        }

    }
})
